package com.yfree.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

import com.yfree.R;

public class YToggleBtn extends LinearLayout {
    private int duration = 100;
    private int toggleWidth = (int) getResources().getDimension(R.dimen.width);
    private int toggleHeight = (int) getResources()
            .getDimension(R.dimen.height);
    private int textPadding = (int) getResources()
            .getDimension(R.dimen.padding);
    private String textOn;
    private String textOff;
    private int textColor = Color.BLACK;
    private boolean checked = true;
    private int toggleImageSelector = R.drawable.togglebutton_selector;
    private int thumbOnImage = R.drawable.progress_thumb_pressed;
    private int thumbOffImage = R.drawable.progress_thumb_off;
    private RelativeLayout.LayoutParams layoutParams;
    private OnCheckedChangeListener listener;
    private ToggleButton btn_backgroud;
    private Button btn_thumb;
    private Context thisContext;
    private boolean firstOpen = true;
    private TranslateAnimation translateAnimation;

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        btn_backgroud.setTextColor(textColor);
    }

    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public int getToggleWidth() {
        return toggleWidth;
    }

    public void setToggleWidth(int toggleWidth) {
        this.toggleWidth = toggleWidth;
        btn_backgroud.setLayoutParams(new RelativeLayout.LayoutParams(
                toggleWidth, toggleHeight));
    }

    public int getToggleHeight() {
        return toggleHeight;
    }

    public void setToggleHeight(int toggleHeight) {
        this.toggleHeight = toggleHeight;
        changeLayoutParams();
    }

    public int getTextPadding() {
        return textPadding;
    }

    public void setTextPadding(int textPadding) {
        this.textPadding = textPadding;
        changePadding(checked);
    }

    public String getTextOn() {
        return textOn;
    }

    public void setTextOn(String textOn) {
        this.textOn = textOn;
        if (checked) {
            btn_backgroud.setText(textOn);
        }
        btn_backgroud.setTextOn(textOn);
    }

    public String getTextOff() {
        return textOff;
    }

    public void setTextOff(String textOff) {
        this.textOff = textOff;
        if (!checked) {
            btn_backgroud.setText(textOff);
        }
        btn_backgroud.setTextOff(textOff);
    }

    public int getToggleImageSelector() {
        return toggleImageSelector;
    }

    public void setToggleImageSelector(int toggleImageSelector) {
        this.toggleImageSelector = toggleImageSelector;
        btn_backgroud.setBackgroundResource(toggleImageSelector);
    }

    public int getThumbOnImage() {
        return thumbOnImage;
    }

    public void setThumbOnImage(int thumbOnImage) {
        this.thumbOnImage = thumbOnImage;
        if (checked) {
            btn_thumb.setBackgroundResource(thumbOnImage);
        }
    }

    public int getThumbOffImage() {
        return thumbOffImage;
    }

    public void setThumbOffImage(int thumbOffImage) {
        this.thumbOffImage = thumbOffImage;
        if (!checked) {
            btn_thumb.setBackgroundResource(thumbOffImage);
        }
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
        btn_backgroud.setChecked(checked);
    }

    public boolean isChecked() {
        return checked;
    }

    public YToggleBtn(Context context) {
        super(context);
        this.thisContext = context;
        inflateContent(context);
    }

    public YToggleBtn(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.thisContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.YToggleBtnAttr);
        duration = typedArray.getInteger(R.styleable.YToggleBtnAttr_duration,
                100);
        toggleWidth = (int) typedArray.getDimension(
                R.styleable.YToggleBtnAttr_toggleWidth, getResources()
                        .getDimension(R.dimen.width));
        toggleHeight = (int) typedArray.getDimension(
                R.styleable.YToggleBtnAttr_toggleHeight, getResources()
                        .getDimension(R.dimen.height));
        textPadding = (int) typedArray.getDimension(
                R.styleable.YToggleBtnAttr_textPadding, getResources()
                        .getDimension(R.dimen.padding));
        textOn = typedArray.getString(R.styleable.YToggleBtnAttr_textOn);
        textOff = typedArray.getString(R.styleable.YToggleBtnAttr_textOff);
        checked = typedArray.getBoolean(R.styleable.YToggleBtnAttr_checked,
                true);
        toggleImageSelector = typedArray.getResourceId(
                R.styleable.YToggleBtnAttr_toggleImageSelector,
                R.drawable.togglebutton_selector);
        thumbOnImage = typedArray.getResourceId(
                R.styleable.YToggleBtnAttr_thumbOnImage,
                R.drawable.progress_thumb_pressed);
        thumbOffImage = typedArray.getResourceId(
                R.styleable.YToggleBtnAttr_thumbOffImage, R.drawable.progress_thumb_off);
        if (typedArray != null) {
            typedArray.recycle();
        }
        if (firstOpen) {
            init(thisContext);
            firstOpen = false;
        }
    }

    private void init(Context context) {
        View toggleView = inflateContent(context);
        if (textOn == null || textOn.length() <= 0) {
            textOn = "默认开";
        }
        if (textOff == null || textOff.length() <= 0) {
            textOff = "默认关";
        }
        btn_backgroud.setTextColor(textColor);
        btn_backgroud.setTextOn(textOn);
        btn_backgroud.setTextOff(textOff);
        layoutParams = (RelativeLayout.LayoutParams) btn_thumb
                .getLayoutParams();
        changeLayoutParams();
        btn_backgroud.setChecked(checked);
        if (checked) {
            isChecked(checked);
        } else {
            isNotChecked(checked);
        }
        btn_backgroud.setBackgroundResource(toggleImageSelector);
        btn_thumb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                btn_backgroud.setChecked(!btn_backgroud.isChecked());
            }
        });
        // ===========================================================
        btn_backgroud.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                checked = isChecked;
                int btn_backgroud_width = buttonView.getWidth();
                int btn_thumb_width = btn_thumb.getWidth();
                if (isChecked) {
                    isChecked(isChecked);
                    translateAnimation = new TranslateAnimation(
                            btn_backgroud_width - btn_thumb_width, 0, 0, 0);
                } else {
                    isNotChecked(isChecked);
                    translateAnimation = new TranslateAnimation(btn_thumb_width
                            - btn_backgroud_width, 0, 0, 0);
                }
                translateAnimation.setDuration(duration);
                translateAnimation.setFillAfter(true);
                btn_thumb.setLayoutParams(layoutParams);
                btn_thumb.startAnimation(translateAnimation);
                if (listener != null) {
                    listener.onCheckedChanged(buttonView, isChecked);
                }
            }
        });
        addView(toggleView);
    }

    private void isChecked(boolean ischecked) {
        changePadding(ischecked);
        btn_backgroud.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        layoutParams.addRule(RelativeLayout.ALIGN_RIGHT);
        layoutParams.addRule(RelativeLayout.ALIGN_LEFT, btn_backgroud.getId());
        btn_thumb.setBackgroundResource(thumbOnImage);
    }

    private void isNotChecked(boolean ischecked) {
        changePadding(ischecked);
        btn_backgroud.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        layoutParams.addRule(RelativeLayout.ALIGN_LEFT);
        layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, btn_backgroud.getId());
        btn_thumb.setBackgroundResource(thumbOffImage);
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        this.listener = listener;
    }

    private View inflateContent(Context context) {
        View toggleView = LayoutInflater.from(context).inflate(
                R.layout.togglebutton_layout, null);
        btn_backgroud = (ToggleButton) toggleView.findViewById(R.id.btn_toggle);
        btn_thumb = (Button) toggleView.findViewById(R.id.btn_thumb);
        return toggleView;
    }

    private void changePadding(boolean ischecked) {
        if (ischecked) {
            btn_backgroud.setPadding(0, 0, textPadding, 0);
        } else {
            btn_backgroud.setPadding(textPadding, 0, 0, 0);
        }
    }

    private void changeLayoutParams() {
        btn_backgroud.setLayoutParams(new RelativeLayout.LayoutParams(
                toggleWidth, toggleHeight));
        layoutParams.width = toggleHeight;
        layoutParams.height = toggleHeight;
        btn_thumb.setLayoutParams(layoutParams);
    }

}
